package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.EmployerCompanyDao;
import com.zhangtai.modules.entity.employee.EmployerCompanyEntity;
import com.zhangtai.modules.service.employee.EmployerCompanyService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class EmployerCompanyServiceImpl extends ServiceImpl<EmployerCompanyDao, EmployerCompanyEntity> implements EmployerCompanyService {

    @Override
    public PageResult queryPage(@RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "representative", required = false) String representative,
                                @RequestParam(value = "registeredAddress", required = false) String registeredAddress,
                                @RequestParam(value = "property", required = false) String property,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<EmployerCompanyEntity> page = this.page(
                new Page<EmployerCompanyEntity>(pageNum,pageSize),
                new QueryWrapper<EmployerCompanyEntity>()
                        .like(!StringUtils.isEmpty(name),"name",name)
                        .like(!StringUtils.isEmpty(representative),"name",representative)
                        .like(!StringUtils.isEmpty(registeredAddress),"name",registeredAddress)
                        .like(!StringUtils.isEmpty(property),"name",property)
                        .eq("is_delete",0).orderByDesc("id")
        );

        return new PageResult(page);
    }

    @Override
    public EmployerCompanyEntity getByIdE(Long id) {
            EmployerCompanyEntity one = this.getOne(new QueryWrapper<EmployerCompanyEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployerCompanyEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployerCompanyEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployerCompanyEntity entity = new EmployerCompanyEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}